package pt.ua.helpdesk.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Class responsible for adding a comment to a ticket
 *  
 * @author Amigos do Noddy
 */
public class CommentAdd extends Composite {
	public CommentAdd()
	{
		
	}
	
	/**
	 * Constructor
	 * 
	 * @param authId ID of the author of the ticket
	 * @param tickId ID of the ticket to which the comment belongs
	 */
	public CommentAdd(Integer authId, Integer tickId) {
		final Integer authorId = authId ;
		final Integer ticketId = tickId ;

		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		initWidget(horizontalPanel);
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final VerticalPanel verticalPanel_1 = new VerticalPanel();
		horizontalPanel.add(verticalPanel_1);
		verticalPanel_1.setStyleName("textbox");

		final VerticalPanel verticalPanel_2 = new VerticalPanel();
		verticalPanel_1.add(verticalPanel_2);

		final Label lblComment = new Label("    Comentário");
		verticalPanel_2.add(lblComment);
		lblComment.setStyleName("labelTicket");

		final VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel_1.add(verticalPanel);
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		final HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_2);
		horizontalPanel_2.setSpacing(2);

		final TextArea txtComment = new TextArea();
		horizontalPanel_2.add(txtComment);
		txtComment.setStyleName("textboxComment");
		txtComment.setSize("300", "150");

		final HorizontalPanel horizontalPanel_3 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_3);
		horizontalPanel_3.setSpacing(2);

		final Button btnSubmeter = new Button();
		horizontalPanel_3.add(btnSubmeter);
		btnSubmeter.setStyleName("button");
		btnSubmeter.setText("Submeter");

		final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_1);

		final Label lblAlerta = new Label("");
		horizontalPanel_1.add(lblAlerta);
		lblAlerta.setStyleName("erro");
		
		btnSubmeter.addClickListener(new ClickListener() {
			public void onClick(final Widget sender){
        		if((txtComment.getText() == "") )
        			lblAlerta.setText("Tem de introduzir os seus Dados!");
        		else
             		ServiceTickets.Util.getInstance().addComment(authorId, ticketId, txtComment.getText(), new AddCommentAsyncCallback(ticketId,lblAlerta));

			}
		}) ;
		
	}
	
	
	/**
	 * AsyncCallback responsible for displaying feedback on whether the comment was added successfully or not
	 * 
	 * @author Amigos do Noddy
	 *
	 */
	static class AddCommentAsyncCallback implements AsyncCallback {
		  Label labelAlerta;
		  Integer ticketId ;
		  
		  /**
		   * Constructor
		   * 
		   * @param label Label where feedback is displayed
		   */
		  
		  public AddCommentAsyncCallback(Integer ticketId, Label labelAlerta) {
			  this.labelAlerta = labelAlerta;
			  this.ticketId = ticketId ;
		  }
		  
		  public void onSuccess(Object object) {
			  Boolean action_result = (Boolean)object;

			  String message;
	    	  if(!action_result.booleanValue())
	    	  {
	    		  message = "Erro a adicionar comentário!";
	    		  DOM.setInnerHTML(labelAlerta.getElement(), message);
	    	  }
	    	  else
	    	  {
	    		  Default.loadComposite(new TicketView(ticketId)) ;
	    	  }
		  }
		  public void onFailure(Throwable throwable) {
			  System.out.println("FALHOU!");
		  }
		}

}
